package com.example.team.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * 此类完成数据上传及下载功能
 * 编写者：mwy
 */
@Controller
public class WordController {
    @GetMapping("/toupfile")
    public String setUpFile(){
        System.out.println("进入访问页面方法");
        return "upfile";
    }
    @PostMapping("/upfile")
    public String upFile(MultipartFile[] fileload, Model model){
        System.out.println("进入上传文件方法");
        model.addAttribute("uploadStatus","上传成功");
        for (MultipartFile file:fileload){
            String filename=file.getOriginalFilename();
            filename= UUID.randomUUID()+"_"+filename;
            String dirPath="D:/000/";
            File filepath=new File(dirPath);
            if (!filepath.exists()){
                filepath.mkdirs();
            }
            try {
                file.transferTo(new File(dirPath+filename));
            }catch (IOException e){
                System.out.println("将上传的文件拷贝到指定位置时报错，原因："+e.getMessage());
                model.addAttribute("uploadStatus","上传失败");
            }
        }
        return "upfile";
    }
}

